package org.alvin.i18n.ui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.houbb.opencc4j.util.ZhConverterUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EasyExcelI18nUtils {

    public static void main(String[] args) throws IOException {
        File dir = new File("E:\\zhifuyunlian\\workspace\\charge\\xiaolancharging\\common\\src\\main\\java\\com\\xl\\common\\exportexcel");
        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".java");
            }
        });

        String regex = "@ExcelProperty\\s?\\(\\s?value\\s?=\\s?\"([^\"]*)\"\\)";
        Map<String, List<String>> dict = Maps.newHashMap();
        for (File file : files) {
            System.out.println(file.getAbsolutePath());
            byte[] buff = Files.readAllBytes(file.toPath());
            String content = new String(buff);
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(content);
            //
            List<String> words = Lists.newArrayList();
            while (matcher.find()) {
                words.add(matcher.group(1));
            }
            System.out.println(words);
            dict.put(file.getName(), words);
        }
        System.out.println("开始映射");
        //开始做映射
        Map<String, String> zhMap = Maps.newLinkedHashMap();
        Map<String, String> twMap = Maps.newLinkedHashMap();
        for (Map.Entry<String, List<String>> entry : dict.entrySet()) {
            String key = entry.getKey();
            File f = new File(dir, key);
            String fileName = f.getName().substring(0, f.getName().indexOf(".") + 1);
            String content = new String(Files.readAllBytes(f.toPath()));
            for (String word : entry.getValue()) {
                String pyKey = Pinyin4jUtil.textToHanYuPinyin(word, HanyuPinyinCaseType.LOWERCASE, HanyuPinyinToneType.WITHOUT_TONE,
                        HanyuPinyinVCharType.WITH_U_AND_COLON);
                pyKey = pyKey.replaceAll("\\W", "");
                String tkey = fileName.concat("header.").concat(pyKey);
                //中文
                zhMap.put(tkey, word);
                //繁体
                twMap.put(tkey, ZhConverterUtil.toTraditional(word));
                //替换内容
                content = content.replace(word, "{" + tkey + "}");
            }
            System.out.println(f.getName());
            System.out.println(content);
            Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> line : zhMap.entrySet()) {
            sb.append(line.getKey());
            sb.append("=");
            sb.append(line.getValue());
            sb.append("\n");
        }
        Files.write(Paths.get("D:", "messages_zh_CN.txt"), sb.toString().getBytes(StandardCharsets.UTF_8));


        sb = new StringBuilder();
        for (Map.Entry<String, String> line : twMap.entrySet()) {
            sb.append(line.getKey());
            sb.append("=");
            sb.append(line.getValue());
            sb.append("\n");
        }
        Files.write(Paths.get("D:", "messages_zh_TW.txt"), sb.toString().getBytes(StandardCharsets.UTF_8));
    }
}
